package it.smlm.playbylocation;

import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;

public class PlayByLocation extends Activity {
	
	private static final String TAG = "PlayByLocation";
	private PlayerManager playerManager;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView position = (TextView) findViewById(R.id.position);

        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        List<String> providers = locationManager.getAllProviders();
        for(String provider : providers) {
        	Log.i(TAG, provider);
        }

        Criteria criteria = new Criteria();
        String bestProvider = locationManager.getBestProvider(criteria, true);
        
        if(bestProvider != null)
        	Log.i(TAG, bestProvider);
        else
        	Log.i(TAG, "bestProvider = null");
        
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if(location == null) {
        	Log.i(TAG, "location = null");
        	position.setText("location = null");
        }
        else {
        	Log.i(TAG, location.toString());
        	position.setText(location.toString());
        }
        
        PlayByLocationListener listener = new PlayByLocationListener();
        listener.setPlayByLocationListener(this);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, listener);
        
        playerManager = PlayerManager.getPlayerManager();
        loadPlayerDS();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	switch(item.getItemId()) {
	    	case R.id.play:
	    		playerManager.startPlayer();	            
	    		break;
	    		
	    	case R.id.pause:
	    		playerManager.pausePlayer();
	    		break;
	    		
	    	case R.id.stop:
	    		playerManager.stopPlayer();
	    		break;
    	}
    	
    	return true;
    }
    
    @Override
    protected void onStop() {
    	playerManager.stopPlayer();
    	playerManager.releasePlayer();
    	
    	super.onStop();
    }
    
//    @Override
//    protected void onStart() {
//    	playerManager.setANewMediaPlayer();
//    	super.onStart();
//    }
    
    private void loadPlayerDS() {
    	AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.lo_chiamavano_trinita);
    	playerManager.setDataSource(afd);
    }
}